package log

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"os"
	"runtime/debug"
	"time"
)

var LogObj *logrus.Logger

type DefaultHook struct {
	logFile *os.File
}

func init() {
	err := os.Mkdir("logs", 0777)
	if err != nil {
		fmt.Println(err)
	}

	file, err := os.Create("logs/" + time.Now().Format("06-01-02 1504") + ".txt")
	if err != nil {
		panic(err)
	}

	LogObj = logrus.New()
	LogObj.SetOutput(file)
	LogObj.AddHook(&DefaultHook{logFile: file})
}

func (h *DefaultHook) Levels() []logrus.Level {
	return []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}
}

func (h *DefaultHook) Fire(entry *logrus.Entry) error {
	fmt.Println(entry.Message)
	if entry.Level != logrus.ErrorLevel {
		debug.PrintStack()
	}
	return nil
}
